package com.example.practice.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.practice.R;
import com.example.practice.activity.ylVideoPlayActivity;
import com.example.practice.bean.VideoBeanYl;

import java.util.List;

public class PlayHistoryAdapterYl extends BaseAdapter {
    private Context ylContext;
    private List<VideoBeanYl> yl_vbl;
    private int[] icon = {R.drawable.video_play_icon1,R.drawable.video_play_icon2,R.drawable.video_play_icon3,
    R.drawable.video_play_icon4,R.drawable.video_play_icon5,R.drawable.video_play_icon6,R.drawable.video_play_icon7,
    R.drawable.video_play_icon8,R.drawable.video_play_icon9,R.drawable.video_play_icon10};

    public PlayHistoryAdapterYl(Context context) {
        this.ylContext=context;
    }

//    设置数据源
    public void setData(List<VideoBeanYl> vbl){
        this.yl_vbl =vbl;
        notifyDataSetChanged();
    }
    class ViewHolder {
        public TextView tv_title, tv_video_title;
        public ImageView iv_icon;
    }

    @Override
    public int getCount() {
        return yl_vbl==null?0:yl_vbl.size();
    }

    @Override
    public VideoBeanYl getItem(int position) {
        return yl_vbl==null?null:yl_vbl.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        if(convertView==null){
            vh = new ViewHolder();
            convertView= LayoutInflater.from(ylContext).inflate(R.layout.yl_history_list_item,null);
            vh.tv_title=convertView.findViewById(R.id.yl_tv_chapter_title);
            vh.tv_video_title=convertView.findViewById(R.id.yl_tv_video_title);
            vh.iv_icon=convertView.findViewById(R.id.yl_iv_video_icon);
            convertView.setTag(vh);
        } else {
            vh= (ViewHolder) convertView.getTag();
        }
        final VideoBeanYl bean = getItem(position);
        if (bean != null) {
            vh.tv_title.setText(bean.title);
            vh.tv_video_title.setText(bean.secondTitle);
            vh.iv_icon.setImageResource(icon[bean.chapterId-1]);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bean==null) return;
//                跳转到播放视频界面
                Intent intent = new Intent(ylContext, ylVideoPlayActivity.class);
                intent.putExtra("videoPath",bean.videoPath);
                ylContext.startActivity(intent);
            }
        });
        return convertView;
    }
}
